package veda;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Comparator;
import java.util.List;

@NoArgsConstructor
@Data
public class VedaResult {

    @JsonProperty("session")
    private SessionDTO session;
    @JsonProperty("FLIGHTS")
    private FLIGHTSDTO flights;

    @NoArgsConstructor
    @Data
    public static class SessionDTO {
        @JsonProperty("channel")
        private String channel;
        @JsonProperty("caching")
        private String caching;
        @JsonProperty("locale")
        private String locale;
        @JsonProperty("engine")
        private String engine;
        @JsonProperty("fqCostTime")
        private Integer fqCostTime;
        @JsonProperty("childPolicy")
        private String childPolicy;
        @JsonProperty("datePolicy")
        private String datePolicy;
        @JsonProperty("ruleDisplayPolicy")
        private String ruleDisplayPolicy;
        @JsonProperty("extraCooperator")
        private ExtraCooperatorDTO extraCooperator;
        @JsonProperty("queryId")
        private String queryId;
        @JsonProperty("getRedisCost")
        private Integer getRedisCost;
        @JsonProperty("decompressCost")
        private Integer decompressCost;
        @JsonProperty("isFqReq")
        private String isFqReq;
        @JsonProperty("isFqHitCache")
        private String isFqHitCache;
        @JsonProperty("isFqSucceed")
        private String isFqSucceed;
        @JsonProperty("isIbeReq")
        private String isIbeReq;
        @JsonProperty("ibeIsResSucceed")
        private String ibeIsResSucceed;
        @JsonProperty("requestTime")
        private String requestTime;
        @JsonProperty("noRefundReissue")
        private Boolean noRefundReissue;
        @JsonProperty("isFis")
        private Boolean isFis;
        @JsonProperty("isRecTop")
        private Boolean isRecTop;
        @JsonProperty("uid")
        private String uid;
        @JsonProperty("useFareCache")
        private Boolean useFareCache;
        @JsonProperty("saleChannel")
        private String saleChannel;
        @JsonProperty("saleScene")
        private String saleScene;
        @JsonProperty("isFisOp")
        private Boolean isFisOp;
        @JsonProperty("experimentId")
        private String experimentId;
        @JsonProperty("algorithmGroup")
        private String algorithmGroup;
        @JsonProperty("snapshotting")
        private Boolean snapshotting;

        @NoArgsConstructor
        @Data
        public static class ExtraCooperatorDTO {
            @JsonProperty("type")
            private String type;
            @JsonProperty("code")
            private List<CodeDTO> code;

            @NoArgsConstructor
            @Data
            public static class CodeDTO {
                @JsonProperty("value")
                private String value;
                @JsonProperty("comparable")
                private Boolean comparable;
                @JsonProperty("fisOpType")
                private String fisOpType;
            }
        }
    }

    @NoArgsConstructor
    @Data
    public static class FLIGHTSDTO {
        @JsonProperty("ID")
        private String id;
        @JsonProperty("fareCacheKey")
        private String fareCacheKey;
        @JsonProperty("avCacheKey")
        private String avCacheKey;
        @JsonProperty("CREATETIME")
        private Long createtime;
        @JsonProperty("SEGMENT")
        private List<SEGMENTDTO> segment;
        @JsonProperty("CITY")
        private List<CITYDTO> city;
        @JsonProperty("AIRPORT")
        private List<AIRPORTDTO> airport;
        @JsonProperty("PLANE")
        private List<PLANEDTO> plane;
        @JsonProperty("CANCACHE")
        private Boolean cancache;

        @NoArgsConstructor
        @Data
        public static class SEGMENTDTO {
            @JsonProperty("DEPCITY")
            private String depcity;
            @JsonProperty("ARRCITY")
            private String arrcity;
            @JsonProperty("DATE")
            private String date;
            @JsonProperty("DATEFLIGHT")
            private DATEFLIGHTDTO dateflight;
            @JsonProperty("ADULTFUELTAX")
            private String adultfueltax;
            @JsonProperty("CHILDFUELTAX")
            private String childfueltax;
            @JsonProperty("INFANTFUELTAX")
            private String infantfueltax;
            @JsonProperty("GMPFUELTAX")
            private String gmpfueltax;
            @JsonProperty("JCPFUELTAX")
            private String jcpfueltax;

            public String id()
            {
                return depcity+arrcity+this.date;
            }
            @NoArgsConstructor
            @Data
            public static class DATEFLIGHTDTO {
                @JsonProperty("FBASIC")
                private FBASICDTO fbasic;
                @JsonProperty("JBASIC")
                private JBASICDTO jbasic;
                @JsonProperty("WBASIC")
                private WBASICDTO wbasic;
                @JsonProperty("YBASIC")
                private YBASICDTO ybasic;
                @JsonProperty("FLIGHT")
                private List<FLIGHTDTO> flight;

                @NoArgsConstructor
                @Data
                public static class FBASICDTO {
                    @JsonProperty("NAME")
                    private String name;
                    @JsonProperty("ADULTPRICE")
                    private Integer adultprice;
                    @JsonProperty("FAREREFERENCE")
                    private String farereference;
                }

                @NoArgsConstructor
                @Data
                public static class JBASICDTO {
                    @JsonProperty("NAME")
                    private String name;
                    @JsonProperty("ADULTPRICE")
                    private Integer adultprice;
                    @JsonProperty("FAREREFERENCE")
                    private String farereference;
                }

                @NoArgsConstructor
                @Data
                public static class WBASICDTO {
                    @JsonProperty("NAME")
                    private String name;
                    @JsonProperty("ADULTPRICE")
                    private Integer adultprice;
                    @JsonProperty("FAREREFERENCE")
                    private String farereference;
                }

                @NoArgsConstructor
                @Data
                public static class YBASICDTO {
                    @JsonProperty("NAME")
                    private String name;
                    @JsonProperty("ADULTPRICE")
                    private Integer adultprice;
                    @JsonProperty("FAREREFERENCE")
                    private String farereference;
                }

                @NoArgsConstructor
                @Data
                public static class FLIGHTDTO {
                    @JsonProperty("SEGINTERVAL")
                    private String seginterval;
                    @JsonProperty("FLIGHTNO")
                    private String flightno;
                    @JsonProperty("AIRLINE")
                    private String airline;
                    @JsonProperty("CODESHARE")
                    private String codeshare;
                    @JsonProperty("CODESHAREINFO")
                    private String codeshareinfo;
                    @JsonProperty("DEPPORT")
                    private String depport;
                    @JsonProperty("ARRPORT")
                    private String arrport;
                    @JsonProperty("DEPTIME")
                    private String deptime;
                    @JsonProperty("ARRTIME")
                    private String arrtime;
                    @JsonProperty("DEPDATE")
                    private String depdate;
                    @JsonProperty("ARRDATE")
                    private String arrdate;
                    @JsonProperty("TIMEDURINGFLIGHT")
                    private String timeduringflight;
                    @JsonProperty("TIMEDURINGFLIGHT_en")
                    private String timeduringflightEn;
                    @JsonProperty("PLANE")
                    private String plane;
                    @JsonProperty("STOPNUMBER")
                    private String stopnumber;
                    @JsonProperty("STOPOVERS")
                    private STOPOVERSDTO stopovers;
                    @JsonProperty("MEAL")
                    private String meal;
                    @JsonProperty("TERM")
                    private String term;
                    @JsonProperty("DEPARTURETERMINAL")
                    private String departureterminal;
                    @JsonProperty("ARRIVALTERMINAL")
                    private String arrivalterminal;
                    @JsonProperty("CABIN")
                    private List<CABINDTO> cabin;
                    @JsonProperty("HALFROUNDCABIN")
                    private List<HALFROUNDCABINDTO> halfroundcabin;
                    @JsonProperty("RATE")
                    private String rate;
                    @JsonProperty("ROUNDTRIPCABIN")
                    private String roundtripcabin;
                    @JsonProperty("MEALINFO")
                    private List<MEALINFODTO> mealinfo;


                    @NoArgsConstructor
                    @Data
                    public static class STOPOVERSDTO {
                        @JsonProperty("STOPOVER")
                        private List<STOPOVERDTO> stopover;

                        @NoArgsConstructor
                        @Data
                        public static class STOPOVERDTO {
                            @JsonProperty("CODE")
                            private String code;
                        }
                    }

                    @NoArgsConstructor
                    @Data
                    public static class CABINDTO {
                        @JsonProperty("originalPrice")
                        private Integer originalPrice;
                        @JsonProperty("BRANDTYPE")
                        private String brandtype;
                        @JsonProperty("NAME")
                        private String name;
                        @JsonProperty("ADULTPRICE")
                        private Integer adultprice;
                        @JsonProperty("CHILDPRICE")
                        private Integer childprice;
                        @JsonProperty("INFANTPRICE")
                        private Integer infantprice;
                        @JsonProperty("DISCOUNT")
                        private String discount;
                        @JsonProperty("ADULTFAREBASIS")
                        private String adultfarebasis;
                        @JsonProperty("CHILDFAREBASIS")
                        private String childfarebasis;
                        @JsonProperty("INFANTFAREBASIS")
                        private String infantfarebasis;
                        @JsonProperty("FAREREFERENCE")
                        private String farereference;
                        @JsonProperty("CHILDFAREREFERENCE")
                        private String childfarereference;
                        @JsonProperty("INFANTFAREREFERENCE")
                        private String infantfarereference;
                        @JsonProperty("GBADULTPRICE")
                        private Integer gbadultprice;
                        @JsonProperty("GBCHILDPRICE")
                        private Integer gbchildprice;
                        @JsonProperty("GBINFANTPRICE")
                        private Integer gbinfantprice;
                        @JsonProperty("CODE")
                        private String code;
                        @JsonProperty("CHILDPRICETYPE")
                        private String childpricetype;
                        @JsonProperty("BASICCABIN")
                        private String basiccabin;
                        @JsonProperty("CODESHARERULE")
                        private String codesharerule;
                        @JsonProperty("ADULTFARECODE")
                        private String adultfarecode;
                        @JsonProperty("ADULTFARERULE")
                        private String adultfarerule;
                        @JsonProperty("CHILDFARECODE")
                        private String childfarecode;
                        @JsonProperty("CHILDFARERULE")
                        private String childfarerule;
                        @JsonProperty("INFANTFARECODE")
                        private String infantfarecode;
                        @JsonProperty("INFANTFARERULE")
                        private String infantfarerule;
                        @JsonProperty("ADULTBAGGAGEALLOWANCE")
                        private String adultbaggageallowance;
                        @JsonProperty("ADULTBAGGAGEALLOWANCEUNIT")
                        private String adultbaggageallowanceunit;
                        @JsonProperty("CHILDBAGGAGEALLOWANCE")
                        private String childbaggageallowance;
                        @JsonProperty("CHILDBAGGAGEALLOWANCEUNIT")
                        private String childbaggageallowanceunit;
                        @JsonProperty("INFANTBAGGAGEALLOWANCE")
                        private String infantbaggageallowance;
                        @JsonProperty("INFANTBAGGAGEALLOWANCEUNIT")
                        private String infantbaggageallowanceunit;
                        @JsonProperty("SECONDPRICE")
                        private List<SecondpriceDTO> secondprice;
                        @JsonProperty("INFO")
                        private String info;
                        public String id()
                        {
                            return name+farereference+adultfarebasis;
                        }

                        @NoArgsConstructor
                        @Data
                        public static class SecondpriceDTO {
                            @JsonProperty("CODE")
                            private String code;
                            @JsonProperty("PRICE")
                            private PRICEDTO price;

                            public String id() {
                                return code+price.id();
                            }
                            @NoArgsConstructor
                            @Data
                            public static class PRICEDTO {
                                @JsonProperty("CHILDFAREBASIS")
                                private String childfarebasis;
                                @JsonProperty("INFANTFAREREFERENCE")
                                private String infantfarereference;
                                @JsonProperty("originalPrice")
                                private Integer originalPrice;
                                @JsonProperty("CHILDFARECODE")
                                private String childfarecode;
                                @JsonProperty("INFANTFARECODE")
                                private String infantfarecode;
                                @JsonProperty("ADULTBAGGAGEALLOWANCEUNIT")
                                private String adultbaggageallowanceunit;
                                @JsonProperty("INFANTBAGGAGEALLOWANCE")
                                private String infantbaggageallowance;
                                @JsonProperty("CHILDFAREREFERENCE")
                                private String childfarereference;
                                @JsonProperty("ADULTFARERULE")
                                private String adultfarerule;
                                @JsonProperty("ADULTBAGGAGEALLOWANCE")
                                private String adultbaggageallowance;
                                @JsonProperty("INFANTBAGGAGEALLOWANCEUNIT")
                                private String infantbaggageallowanceunit;
                                @JsonProperty("CHILDBAGGAGEALLOWANCEUNIT")
                                private String childbaggageallowanceunit;
                                @JsonProperty("CHILD")
                                private Integer child;
                                @JsonProperty("CHILDBAGGAGEALLOWANCE")
                                private String childbaggageallowance;
                                @JsonProperty("ADULT")
                                private Integer adult;
                                @JsonProperty("NAME")
                                private String name;
                                @JsonProperty("DISCOUNT")
                                private String discount;
                                @JsonProperty("FAREBASIS")
                                private String farebasis;
                                @JsonProperty("FAREREFERENCE")
                                private String farereference;
                                @JsonProperty("ADULTFARECODE")
                                private String adultfarecode;
                                @JsonProperty("INFANTFAREBASIS")
                                private String infantfarebasis;
                                @JsonProperty("INFANTFARERULE")
                                private String infantfarerule;
                                @JsonProperty("INFANT")
                                private Integer infant;
                                @JsonProperty("CHILDFARERULE")
                                private String childfarerule;
                                public String id() {
                                    return  name+farereference+adultfarecode;
                                }
                            }
                        }
                    }
                    }

                    @NoArgsConstructor
                    @Data
                    public static class HALFROUNDCABINDTO {
                        @JsonProperty("NAME")
                        private String name;
                        @JsonProperty("INFO")
                        private String info;
                    }

                    @NoArgsConstructor
                    @Data
                    public static class MEALINFODTO {
                        @JsonProperty("DEP")
                        private String dep;
                        @JsonProperty("ARR")
                        private String arr;
                        @JsonProperty("MEAL")
                        private List<MEALDTO> meal;

                        @NoArgsConstructor
                        @Data
                        public static class MEALDTO {
                            @JsonProperty("CABIN")
                            private String cabin;
                            @JsonProperty("MEALNAMEZH")
                            private String mealnamezh;
                            @JsonProperty("MEALNAMEEN")
                            private String mealnameen;
                        }
                    }
                }
            }
        }

        @NoArgsConstructor
        @Data
        public static class CITYDTO {
            @JsonProperty("CODE")
            private String code;
            @JsonProperty("ZHNAME")
            private String zhname;
            @JsonProperty("ENNAME")
            private String enname;
        }

        @NoArgsConstructor
        @Data
        public static class AIRPORTDTO {
            @JsonProperty("CODE")
            private String code;
            @JsonProperty("ZHNAME")
            private String zhname;
            @JsonProperty("ENNAME")
            private String enname;
            @JsonProperty("CITY")
            private String city;
        }

        @NoArgsConstructor
        @Data
        public static class PLANEDTO {
            @JsonProperty("CODE")
            private String code;
            @JsonProperty("ZHNAME")
            private String zhname;
            @JsonProperty("ENNAME")
            private String enname;
            @JsonProperty("AIRPORTTAX")
            private String airporttax;
            public String id()
            {
                return this.code+this.zhname+this.enname+this.airporttax;
            }
        }


    public void sort()
    {
        if(this.getFlights().getPlane()!=null) {
            this.getFlights().getPlane().sort(Comparator.comparing(PLANEDTO::id));
        }
        this.getFlights().getSegment().sort(Comparator.comparing(FLIGHTSDTO.SEGMENTDTO::id));
        this.getFlights().getSegment().forEach(segmentdto -> {
            segmentdto.getDateflight().getFlight().sort(Comparator.comparing(FLIGHTSDTO.SEGMENTDTO.DATEFLIGHTDTO.FLIGHTDTO::getFlightno));
            segmentdto.getDateflight().getFlight().forEach(flightdto -> {
                flightdto.cabin.sort(Comparator.comparing(FLIGHTSDTO.SEGMENTDTO.DATEFLIGHTDTO.FLIGHTDTO.CABINDTO::id));
                //二次运价排序
                if(flightdto.cabin!=null) {
                    flightdto.cabin.forEach(cabindto -> {
                        if(cabindto.secondprice!=null) {
                            cabindto.secondprice.sort(Comparator.comparing(FLIGHTSDTO.SEGMENTDTO.DATEFLIGHTDTO.FLIGHTDTO.CABINDTO.SecondpriceDTO::id));
                        }
                    });
                }
            });
        });
    }
}
